<?php
namespace App\Model\Backstage;
use App\Model\Base;
use Illuminate\Support\Facades\DB;
use App\Model\Backstage\Achievement;
use Illuminate\Http\Request;

/**
 * Class ContractEarnestMoney
 * @package App\Model\Backstage
 * 合同诚意金model
 */
class ContractEarnestMoney extends Base
{
    //合同诚意金数据表
    protected  $table;
    public function __construct()
    {
        parent::__construct();
        $this->table = 'contract_earnest_money_'.$this->branch_id;  //后期分表
//        $this->table = 'contract_earnest_money';
    }

    //交易类型
    public static $type_arr = ['出租', '出售'];
    //当前状态
    public static $status_arr = ['已收','转定', '入库', '已退'];
    //付款方式
    public static $pay_type_arr = ['现金', '支票', '转账', '汇款'];

    /**
     * @param $params
     * @return mixed
     * 添加诚意金
     */
    public function addPrice($params){
        $params['e_date'] = strtotime($params['e_date']);
        $params['e_created_at'] = time();
        $params['e_updated_at'] = time();
        return DB::table($this->table)->insert($params);
    }

    /**
     * @param $params
     * @return mixed
     * 编辑诚意金
     */
    public function editPrice($params){
        $params['e_date'] = strtotime($params['e_date']);
        $params['e_updated_at'] = time();
        return DB::table($this->table)->where('e_id', $params['e_id'])->update($params);
    }

    /**
     * @param $e_id
     * @return mixed
     * 删除诚意金记录
     */
    public function delPrice($e_id){
        return DB::table($this->table)->where('e_id', $e_id)->delete();
    }

    /**
     * @param $map
     * @param $keywords
     * @param int $pageLimit
     * @return bool
     * 查找诚意金列表
     */
    public function lists($map, $keywords, $pageLimit = 20){
        $field = [
            'e_id', 'e_r_num', 'e_type', 'e_intentional_money', 'e_use_id', 'e_r_address', 'e_status',
            'e_owner_name', 'e_owner_phone', 'e_owner_tel', 'e_owner_address',
            'e_owner_idnum', 'e_customer_name', 'e_customer_phone', 'e_customer_tel',
            'e_customer_address', 'e_customer_idnum', 'e_price', DB::raw('from_unixtime(e_date) as e_date'), 'e_receipt_num',
            'e_pay_type', 'e_d_id', 'e_uid', 'e_refund', 'e_remark',
            DB::raw('from_unixtime(e_created_at) as e_created_at'),DB::raw('from_unixtime(e_updated_at) as e_updated_at'), 'u_username',
            'name'
        ];
        $list = DB::table($this->table)
            ->leftjoin('user', 'e_uid', '=', 'u_id')
            ->leftJoin('department', 'e_d_id', '=', 'department.id')
            ->where(function ($query) use($map, $keywords){
                $query->where($map)->where('e_owner_name', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($map, $keywords) {
                $query->where($map)->where('e_owner_phone', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($map, $keywords){
                $query->where($map)->where('e_customer_name', 'like', '%'.$keywords.'%');
            })->orWhere(function ($query) use($map, $keywords){
                $query->where($map)->where('e_customer_phone', 'like', '%'.$keywords.'%');
            })
            ->select($field)
            ->orderBy('e_id', 'desc')
            ->paginate($pageLimit);
        if (sizeof($list) == 0) {
            return false;
        }
        return $list;
    }
}